//
//  ExampleViewController.m
//  SegmentView
//
//  Created by tom.sun on 16/5/27.
//  Copyright © 2016年 tom.sun. All rights reserved.
//
#define TOPICCELL @"topicCell"
#import "ExampleViewController.h"
#import "UIView+UPView.h"
#import <AFNetworking.h>
#import "TopicCellModel.h"
#import "TopicTableViewCell.h"
#import "TopicDetailViewController.h"
@interface ExampleViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, copy) NSString *titleStr;
@property(nonatomic,strong)NSMutableArray *goodsArray;
@property(nonatomic,strong)UITableView *tabelView;
@end

@implementation ExampleViewController
-(NSMutableArray *)goodsArray{
    if (_goodsArray==nil) {
        _goodsArray=[NSMutableArray array];
    }
    return _goodsArray;
}
- (instancetype)initWithIndex:(NSInteger)index title:(NSString *)title
{
    self = [super init];
    if (self) {
        _titleStr = title;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self sendUrlRequest];
    // Do any additional setup after loading the view.
}

- (void)setupUI
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.height = tableView.height - 50 - 64;
    tableView.delegate = self;
    tableView.dataSource = self;
    UINib *nib=[UINib nibWithNibName:@"TopicTableViewCell" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:TOPICCELL];
    self.tabelView=tableView;
    [self.view addSubview:tableView];
}
-(void)sendUrlRequest{
    [self.goodsArray removeAllObjects];
    NSString *urlStr =[NSString stringWithFormat:@"http://zhekou.repai.com/lws/wangyu/index.php?control=tm&model=ws&tabId=%@&page=1&app_id=3941783725&app_oid=67341bb7a80f955a83e8e590931f70b5&app_version=1.1.1&app_channel=iphoneappstore&shce=haolemai",self.titleStr];
    AFHTTPSessionManager *manage=[AFHTTPSessionManager manager];
    [manage GET:urlStr parameters:nil success:^(NSURLSessionDataTask *task, NSDictionary *responseDict) {
        [responseDict writeToFile:@"/Users/wyzc/Desktop/ajsfasb.plist" atomically:YES];
        NSArray *dataArray=responseDict[@"data"];
        for (NSDictionary *dict in dataArray) {
            TopicCellModel *model=[TopicCellModel cellModelWithDict:dict];
            [self.goodsArray addObject:model];
        }
        [self.tabelView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.goodsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicCellModel *model=self.goodsArray[indexPath.row];
    
    TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TOPICCELL];
//    if (cell == nil) {
//        cell = [[ TopicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TOPICCELL];
//    }
    cell.model=model;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TopicCellModel *model=self.goodsArray[indexPath.row];
    TopicDetailViewController *detailVC=[[TopicDetailViewController alloc]init];
    detailVC.model=model;
    [self.navigationController pushViewController:detailVC animated:YES];
}
@end
